#include "QFileProperty.h"
#include <QFileInfo>
#include <QAbstractItemDelegate>

#include "QFilePropertyEditor.h"

#include "private/QFileProperty_p.h"

using namespace QPE;

QFileProperty::QFileProperty(const QString& name)
	: QProperty(new QFilePropertyPrivate(name, QVariant::String))
{

}

QFileProperty::~QFileProperty()
{
	//
}


void QFileProperty::setFileFilters(const QString& filefilters)
{
	static_cast<QFilePropertyPrivate*>(d_ptr)->FileFilters = filefilters;
}


QString QFileProperty::getFileFilters() const
{
	return static_cast<QFilePropertyPrivate*>(d_ptr)->FileFilters;
}


void QFileProperty::setFile(const QString& file)
{
	d_ptr->VariantValue.setValue(file);
}


QString QFileProperty::getFile() const
{
	return d_ptr->VariantValue.toString();
}


QVariant QFileProperty::data (int column, int role) const
{
	if(column == DPC_Data && (Qt::DisplayRole == role || Qt::EditRole == role))
	{
		QFileInfo tmpFile(d_ptr->VariantValue.toString());
		return tmpFile.fileName();
	}
	else
		return QProperty::data(column, role);
}


QWidget* QFileProperty::createEditor(QWidget * parent, const QStyleOptionViewItem& options, const QAbstractItemDelegate* delegate)
{
	Q_UNUSED(options);

	QFileEditWidget* tmpWidget = new QFileEditWidget(parent);
	if(delegate)
		QFileEditWidget::connect(tmpWidget, SIGNAL(commitData(QWidget*)), delegate, SIGNAL(commitData(QWidget*)));
	tmpWidget->setFilter(static_cast<QFilePropertyPrivate*>(d_ptr)->FileFilters);	// todo: parse this string
	tmpWidget->setFile(d_ptr->VariantValue.toString());
	tmpWidget->setDirectory(static_cast<QFilePropertyPrivate*>(d_ptr)->Directory);
	return tmpWidget;
}


bool QFileProperty::setEditorData(QWidget* editor)
{
	QFileEditWidget* tmpWidget = qobject_cast<QFileEditWidget*>(editor);
	if(tmpWidget)
		tmpWidget->setFile(d_ptr->VariantValue.toString());
	else
		return false;

	return true;
}


QVariant QFileProperty::getEditorData(QWidget* editor) const
{
	QFileEditWidget* tmpWidget = qobject_cast<QFileEditWidget*>(editor);
	if(tmpWidget)
		return tmpWidget->getFile();

	return QVariant();
}

void QFileProperty::setSetting(const QString& key, const QVariant& value)
{
	if(key == "FileFilters")
		setFileFilters(value.toString());
	else if(key == "Directory")
		setDirectory(value.toBool());
}

QVariant QFileProperty::getSetting(const QString& key) const
{
	if(key == "FileFilters")
		return getFileFilters();
	else if(key == "Directory")
		return isDirectory();
	else
		return QProperty::getSetting(key);
}

QStringList QFileProperty::getSettingKeys() const
{
	return QStringList("FileFilters") << "Directory";
}

QString QFileProperty::type() const
{
	return "file";
}

QProperty* QFileProperty::clone(bool include_children, QProperty* container) const
{
	return QProperty::clone(include_children, container ? container : new QFileProperty(getName()));
}

bool QFileProperty::isDirectory() const
{
	return static_cast<QFilePropertyPrivate*>(d_ptr)->Directory;
}


void QFileProperty::setDirectory(bool is_directory)
{
	static_cast<QFilePropertyPrivate*>(d_ptr)->Directory = is_directory;
}
